﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockManagement.ViewModels;
using System.Collections.ObjectModel;
using StockManagement.Commands;
using StockManagement.Helper;
using StockManagement.Models;
using System.Windows;

namespace StockManagement.ViewModels
{
    public class ProductGridViewModel : BaseViewModel
    {
        ObservableCollection<ProductViewModel> _allProductCollection = new ObservableCollection<ProductViewModel>();
        ObservableCollection<ProductViewModel> _productCollection = new ObservableCollection<ProductViewModel>();
        DelegateCommand _ExportToExcelCommand;
        DGShowProduct _shwProduct;

        public ProductGridViewModel(ObservableCollection<ProductViewModel> allProductCollection)
        {
            _allProductCollection = allProductCollection;
            allProductCollection.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(allProductCollection_CollectionChanged);
            _productCollection = new ObservableCollection<ProductViewModel>();
            _ExportToExcelCommand = new DelegateCommand(ExecuteExportToExcel);
            PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(ProductGridViewModel_PropertyChanged);
            ShowProduct = DGShowProduct.All;
        }

        void allProductCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                    if (e.NewItems.Count < 1)
                        return;

                    SetProductCollection();
                    break;
            }
        }

        void ProductGridViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "ShowProduct":
                    SetProductCollection();
                    break;
            }
        }

        private void SetProductCollection()
        {
            switch (_shwProduct)
            {
                case DGShowProduct.All:
                    var coll = AllProductCollection.AsEnumerable();
                    ProductCollection = new ObservableCollection<ProductViewModel>(coll);
                    ShowPurchaser = Visibility.Visible;
                    ShowConsumer = Visibility.Visible;
                    break;
                case DGShowProduct.Defined:
                    coll = AllProductCollection.Where(p => p.ProductStatus == ProductStatus.Defined);
                    ProductCollection = new ObservableCollection<ProductViewModel>(coll);
                    ShowPurchaser = Visibility.Collapsed;
                    ShowConsumer = Visibility.Collapsed;
                    break;
                case DGShowProduct.Inward_InProgress:
                    coll = AllProductCollection.Where(p => p.ProductStatus == ProductStatus.Inward_InProgress);
                    ProductCollection = new ObservableCollection<ProductViewModel>(coll);
                    ShowPurchaser = Visibility.Collapsed;
                    ShowConsumer = Visibility.Visible;
                    break;
                case DGShowProduct.Outward_ToBeDeliverd:
                    coll = AllProductCollection.Where(p => p.ProductStatus == ProductStatus.Outward_ToBeDeliverd);
                    ProductCollection = new ObservableCollection<ProductViewModel>(coll);
                    ShowPurchaser = Visibility.Collapsed;
                    ShowConsumer = Visibility.Visible;
                    break;
                case DGShowProduct.Delivered:
                    coll = AllProductCollection.Where(p => p.ProductStatus == ProductStatus.Delivered);
                    ProductCollection = new ObservableCollection<ProductViewModel>(coll);
                    ShowPurchaser = Visibility.Visible;
                    ShowConsumer = Visibility.Visible;
                    break;
            }
        }

        Visibility _ShowPurchaser;
        public Visibility ShowPurchaser
        {
            get { return _ShowPurchaser; }
            set
            {
                _ShowPurchaser = value;
                OnPropertyChanged("ShowPurchaser");
            }
        }

        Visibility _ShowConsumer;
        public Visibility ShowConsumer
        {
            get { return _ShowConsumer; }
            set
            {
                _ShowConsumer = value;
                OnPropertyChanged("ShowConsumer");
            }
        }

        public ObservableCollection<ProductViewModel> AllProductCollection
        {
            get
            {
                return _allProductCollection;
            }
        }

        public ObservableCollection<ProductViewModel> ProductCollection
        {
            get { return _productCollection; }
            set
            {
                _productCollection = value;
                OnPropertyChanged("ProductCollection");
            }
        }

        public DGShowProduct ShowProduct
        {
            get { return _shwProduct; }
            set
            {
                _shwProduct = value;
                OnPropertyChanged("ShowProduct");
            }
        }

        public DelegateCommand ExportToExcelCommand
        {
            get
            {
                return _ExportToExcelCommand;
            }
        }

        private void ExecuteExportToExcel(object item)
        {
            ExportToExcel<ProductViewModel, List<ProductViewModel>> exportToExcel = new ExportToExcel<ProductViewModel, List<ProductViewModel>>();
            exportToExcel.dataToPrint = _productCollection.ToList();
            exportToExcel.GenerateReport();
        }

        internal void RaisePropertyChanged()
        {
            OnPropertyChanged("ShowProduct");
        }
    }
}
